import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.*;
/**
 * This class will represent the medical records
 * of the Doctor's Office System.
 * A medical record consists of all the treatment records under a single patient's name.
 * @author John Dowling (jdowling6@gatech.edu), Michael Zipp
 * Team: 1-Thunder/Dragon
 * @version 1.0
 */

public class MedicalRecord implements Serializable{
	private List<TreatmentRecord> tr;
	public Patient patient;
	
	private static Logger medicalRecordLog = 
		Logger.getLogger(MedicalRecord.class.getName());
	
	/**
	 * MedicalRecord will take in a List of TreatmentRecords. 
	 * @param tr The list of TreatmentRecords.
	 */
	
	static{
		ConsoleHandler console = new ConsoleHandler();
		console.setLevel(Level.FINEST);
		console.setFormatter(new ThunderDragonHtmlFormatter());
		medicalRecordLog.addHandler(console);
		medicalRecordLog.setLevel(Level.ALL);
		
		try {
			FileHandler fh = new FileHandler("MedicalRecordLog.txt");
			fh.setFormatter(new ThunderDragonHtmlFormatter());
			medicalRecordLog.addHandler(fh);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			medicalRecordLog.severe("Failure: Security exception thrown" +
					" when trying to create a new handler " +
					"in MedicalRecord.java.");
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			medicalRecordLog.severe("Failure: I/O exception thrown" +
					" when trying to create a new handler" +
					" in MedicalRecord.java.");
			e.printStackTrace();
		}
		
	}

	public MedicalRecord(List<TreatmentRecord> tr, Patient p){
		medicalRecordLog.entering(this.getClass().getName(), 
				"Medical Record Constructor", new Object[] {tr, p});
		if(tr != null){
			this.tr = tr;
			this.patient = p;
		}
		else{
			medicalRecordLog.warning("Null data was passed in MedicalRecord Constructor");
		}
		medicalRecordLog.exiting(this.getClass().getName(), "MedicalRecord Constructor");
	}
	
	public MedicalRecord(Patient p){
		this.patient = p;
		medicalRecordLog.finest("Got the MedicalRecord.");
	}
	
	/**
	 * Will find all treatment records pertaining to a certain Doctor. 
	 * @param doctorUsername The specified Doctor.
	 * @return tempList A list of treatment records.
	 */
	
	public List<TreatmentRecord> findByDoctor(String doctorUsername){
		
		List<TreatmentRecord> tempList = new ArrayList<TreatmentRecord>();
		for(TreatmentRecord record : tr){
			if(record.getDoctor().getUsername().equals(doctorUsername)){
				tempList.add(record);
			}
		}
		medicalRecordLog.finest("Found the list of treatment records by doctor.");
		return tempList;
		
	}
	
	/**
	 * Returns a list of treatment records pertaining to a
	 * specified date.
	 * @param date The specified date.
	 * @return tempList A list of treatment records.
	 */

	public List<TreatmentRecord> findByDate(String date){
		List<TreatmentRecord> tempList = new ArrayList<TreatmentRecord>();
		for(TreatmentRecord record : tr){
			if(record.getDate().equals(date)){ //By command of the Code Audit.
			//	if(record.getDate() == date){
				tempList.add(record);
			}
		}
		medicalRecordLog.finest("Found the list of treatment records by date.");
		return tempList;
	}
	
	public List<TreatmentRecord> findByMonth(int month){
		List<TreatmentRecord> tempList = new ArrayList<TreatmentRecord>();
		for(TreatmentRecord record : tr){
			if(record.getMonth()==month){ //By command of the Code Audit.
				tempList.add(record);
			}
		}
		return tempList;
	}
	/*
	/**
	 * Returns a list of treatment records pertaining to a
	 * specified patient.
	 * @param p The specified patient.
	 */
	
	//Matt: I don't think this is necessary because all treatment records are already under
	//a specific patient.
	/*
	public List<TreatmentRecord> findByPatient(Patient p){
		if( p != null){
			List<TreatmentRecord> tempList = new ArrayList<TreatmentRecord>();
			for(TreatmentRecord record : tr){
				if(record.patient.equals(p)){
					tempList.add(record);
				}
			}
			return tempList;
		}
		medicalRecordLog.finest("Found the list of treatment records by patient.");
		return null;
	}
*/
	
	public void addTreatmentRecord(TreatmentRecord tr) {
		this.tr.add(tr);
		medicalRecordLog.finest("Added the treatment record.");
	}

	@Override
	public String toString() {
		return "MedicalRecord";
	}
	
	
}
